package com.activenetwork.smlite.testcase.events.createEvents.selectComponents;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.custom.other.XCheckbox;
import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class SelectComponents003 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(SelectComponents003.class);
	
	@Test
	public void Verify_Checkbox_in_event_setting_section_works_correctly(){
		loginBusiness.login();
		EventsSnippet.gotoAddEventPage();
		WebDriver driver = EnvContext.webDriverTL.get();
		List<WebElement> checkboxes = driver.findElements(By.xpath("//div[contains(@class,'autotest-html-eventbasicsubview')]//p[@class='mouseoverClass']/input"));
		
		// Verify default checked : Registration Form, Theme Designer,  Reports
		for(WebElement checkbox : checkboxes){
			String componentName = checkbox.getAttribute("data-component-name").trim();
			if(componentName.equals("venueSourcing")){
				checkDefaultReadOnlyNot(checkbox, "Sourcing");
			}else if(componentName.equals("socialMedia")){
				checkDefaultReadOnlyNot(checkbox, "Social Media");
			}else if(componentName.equals("eventWebsite")){
				checkDefaultReadOnlyNot(checkbox, "Website");
			}else if(componentName.equals("email")){
				checkDefaultReadOnlyNot(checkbox, "Email");
			}else if(componentName.equals("attendeeDirectories")){
				checkDefaultReadOnlyNot(checkbox, "Attendee Directories");
			}else if(componentName.equals("registrationform")){
				checkDefaultReadOnly(checkbox, "Registration Form");
			}else if(componentName.equals("themedesigner")){
				checkDefaultReadOnly(checkbox, "Theme Designer");
			}else if(componentName.equals("mobile")){
				checkDefaultReadOnlyNot(checkbox, "Mobile");
			}else if(componentName.equals("labelAndBadges")){
				checkDefaultReadOnlyNot(checkbox, "Label & Badges");
			}else if(componentName.equals("kiosk")){
				checkDefaultReadOnlyNot(checkbox, "Kiosk");
			}else if(componentName.equals("reports")){
				checkDefaultReadOnly(checkbox, "Reports");
			}else if(componentName.equals("customReports")){
				checkDefaultReadOnlyNot(checkbox, "Custom Reports");
			}else if(componentName.equals("surveys")){
				checkDefaultReadOnlyNot(checkbox, "Surveys");
			}else{
				EnvContext.failurePointTL.get().add(new FailurePoint(
						SelectComponents003.class.getName(), 
						"Verify checkbox default checked and readonly", 
						componentName + " is unknown for checking"));
			}
		}
		
		// Verify components will update left navigation part
		for(WebElement checkbox : checkboxes){
			XCheckbox xcheckbox = new XCheckbox(checkbox);
			String componentName = xcheckbox.getAttribute("data-component-name").trim();
			if(componentName.equals("venueSourcing")){
				By by = By.xpath("//div[@id='collapse-planning']//a[text()='Sourcing']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Sourcing");
			}else if(componentName.equals("socialMedia")){
				By by = By.xpath("//div[@id='collapse-promotion']//a[text()='Social Media']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Social Media");
			}else if(componentName.equals("eventWebsite")){
				By by = By.xpath("//div[@id='collapse-promotion']//a[text()='Website']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Website");
			}else if(componentName.equals("email")){
				By by = By.xpath("//div[@id='collapse-promotion']//a[text()='Email Invitations']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Email");
			}else if(componentName.equals("attendeeDirectories")){
				By by = By.xpath("//div[@id='collapse-promotion']//a[text()='Attendee Directories']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Attendee Directories");
			}else if(componentName.equals("registrationform")){
				// Registration Form is by default checked on and read-only
				// so registration link is always displaying
				//By by = By.xpath("//div[@id='collapse-registration']//a[text()='Registration Form']");
				//checkLeftNavigationUpdate(driver, xcheckbox, by, "Registration Form");
			}else if(componentName.equals("themedesigner")){
				// Theme Designer is by default checked on and read-only
				// so theme designer link is always displaying
				//By by = By.xpath("//div[@id='collapse-registration']//a[text()='Theme Designer']");
				//checkLeftNavigationUpdate(driver, xcheckbox, by, "Theme Designer");
			}else if(componentName.equals("mobile")){
				By by = By.xpath("//div[@id='collapse-onsite']//a[text()='Mobile']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Mobile");
			}else if(componentName.equals("labelAndBadges")){
				By by = By.xpath("//div[@id='collapse-onsite']//a[text()='Badges & Labels']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Badges & Labels");
			}else if(componentName.equals("kiosk")){
				By by = By.xpath("//div[@id='collapse-onsite']//a[text()='Kiosk']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Kiosk");
			}else if(componentName.equals("reports")){
				// Reports is by default checked on and read-only
				// so reports link is always displaying
				//By by = By.xpath("//div[@id='collapse-analysis']//a[text()='Reports']");
				//checkLeftNavigationUpdate(driver, xcheckbox, by, "Reports");
			}else if(componentName.equals("customReports")){
				By by = By.xpath("//div[@id='collapse-analysis']//a[text()='Custom Reports']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Custom Reports");
			}else if(componentName.equals("surveys")){
				By by = By.xpath("//div[@id='collapse-analysis']//a[text()='Surveys']");
				checkLeftNavigationUpdate(driver, xcheckbox, by, "Survyes");
			}else {
				EnvContext.failurePointTL.get().add(new FailurePoint(
						SelectComponents003.class.getName(), 
						"Verify check on component display left navigation", 
						componentName + " is unknown for checking"));
			}
		}
		
		
		// Verify the next step change with components
		for(WebElement checkbox : checkboxes){
			XCheckbox xcheckbox = new XCheckbox(checkbox);
			String componentName = xcheckbox.getAttribute("data-component-name").trim();
			if(componentName.equals("venueSourcing")){
				checkNextStepLabel(xcheckbox, "Sourcing", "Soucing");
			}
			if(componentName.equals("socialMedia")){
				checkNextStepLabel(xcheckbox, "Social", "Social Media");
			}
			if(componentName.equals("eventWebsite")){
				checkNextStepLabel(xcheckbox, "Website", "Website");
			}
			if(componentName.equals("email")){
				checkNextStepLabel(xcheckbox, "Email", "Email");
			}
			if(componentName.equals("attendeeDirectories")){
				checkNextStepLabel(xcheckbox, "Directories", "Attendee Directories");
			}
		}
		
		
		
		loginBusiness.logout();
		FailureAssertion.pop(SelectComponents003.class);
	}
	
	private void checkDefaultReadOnly(WebElement checkbox, String componentName){
		String checked = checkbox.getAttribute("checked");
		if(!StringUtils.equalsIgnoreCase(checked, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default checked", 
					componentName + " is not checked by default"));
		}
		String readonly = checkbox.getAttribute("readonly");
		if(!StringUtils.equalsIgnoreCase(readonly, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default read-only", 
					componentName + " is not read-only"));
		}
	}
	
	private void checkDefaultReadOnlyNot(WebElement checkbox, String componentName){
		String checked = checkbox.getAttribute("checked");
		if(StringUtils.equalsIgnoreCase(checked, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default checked", 
					componentName + " is checked by default"));
		}
		String readonly = checkbox.getAttribute("readonly");
		if(StringUtils.equalsIgnoreCase(readonly, "true")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify checkbox default read-only", 
					componentName + " is read-only"));
		}
	}
	
	
	
	private void checkLeftNavigationUpdate(WebDriver driver, XCheckbox checkbox, By leftNavigationLinkBy, String componentName){
		// Verify left navigation display when checked on
		checkbox.checkON();
		String value1 = driver.findElement(leftNavigationLinkBy).getAttribute("class");
		if(StringUtils.containsIgnoreCase(value1, "hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify check on component display left navigation", 
					componentName + " checked on but left navigation does not display"));
		}
		// Verify left navigation hide when checked off
		checkbox.checkOFF();
		String value2 = driver.findElement(leftNavigationLinkBy).getAttribute("class");
		if(!StringUtils.containsIgnoreCase(value2, "hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify check off component hide left navigation", 
					componentName + " checked off but left navigation does not hide"));
		}
	}
	
	private void checkNextStepLabel(XCheckbox checkbox, String expectedLabel, String componentName){
		checkbox.checkON();
		WebElement nextStep = IdentifierFactory.createWebElement("EventBasicSubview", "nextStep");
		String label = nextStep.findElement(By.xpath("./span")).getText().trim();
		if(!StringUtils.equalsIgnoreCase(label, expectedLabel)){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents003.class.getName(), 
					"Verify component update next step label", 
					componentName + " checked on but left navigation does not change to " + expectedLabel));
		}
		checkbox.checkOFF();
	}
}
